package com.rmb.controller;


import com.rmb.annotation.SystemLog;
import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.entity.ResponeRest;
import com.rmb.entity.SourceEntity;
import com.rmb.entity.result.AddDataSourceResult;
import com.rmb.enums.LogType;
import com.rmb.pool.RedisConnention;
import com.rmb.service.SourceService;
import com.rmb.util.JdbcConnector;
import com.rmb.util.RedisConnector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoMuYang
 * @Date 2021/1/6 19:46
 */
@RestController
@RequestMapping("/rmb/source")
public class SourceController {
    @Autowired
    private SourceService sourceService;

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @SystemLog(description = "根据ID删除",type = LogType.DELETE)
    public ResponseResult delSql(@PathVariable("id") Long id) {
        SourceEntity sourceEntity = sourceService.getsql(id);
        if (sourceEntity != null) {
            sourceService.delte(id);
            return new ResponseResult(CommonCode.SUCCESS);
        } else {
            return new ResponseResult(CommonCode.FAIL);
        }
    }

    @PostMapping("/addSource")
    @SystemLog(description = "添加数据源",type = LogType.INSERT)
    public SourceEntity addSource(@RequestBody SourceEntity sourceEntity) throws Exception {
        System.out.println(sourceEntity);
        return sourceService.addSource(sourceEntity);
    }

    /**
     * 测试
     *
     * @param id 测试数据源ID
     * @return 测试结果
     */
    @GetMapping("/testHeartbeat/{id}")
    @SystemLog(description = "测试数据源",type = LogType.OTHER_OPERATIONS)
    public ResponseResult testHeartbeat(@PathVariable("id") Long id) {
        return sourceService.testHeartbeat(id);
    }


    /**
     * 回显数据源数据
     * @param id
     * @param flag
     * @return
     */
    @GetMapping("/findSourceById/{id}/{flag}")
    @SystemLog(description = "回显数据源数据",type = LogType.OTHER_OPERATIONS)
    public ResponeRest findSourceById(@PathVariable("id") Long id,
                                      @PathVariable("flag") String flag) {
        return sourceService.findSourceById(id, flag);
    }

    /**
     * 修改数据源信息
     *
     * @param sourceEntity
     */
    @PostMapping("/update")
    @SystemLog(description = "修改数据源信息",type = LogType.UPDATE)
    public void update(@RequestBody SourceEntity sourceEntity) {
        sourceService.update(sourceEntity);
    }

    /**
     * 数据源列表
     *
     * @return findList
     */
    @GetMapping("/findList")
    @SystemLog(description = "查询数据源",type = LogType.SELETE)
    public List<SourceEntity> findList() {
        return sourceService.findList();
    }

    /**
     * 获取请求数据
     *
     * @param id
     * @return findById
     */
    @GetMapping("/findById/{id}")
    @SystemLog(description = "获取请求数据",type = LogType.OTHER_OPERATIONS)
    public AddDataSourceResult findById(@PathVariable("id") Long id) {
        return sourceService.findById(id);
    }

    /**
     * 修改数据源
     *
     * @param sourceEntity
     * @param id
     * @return
     */
    @PutMapping("/updateDataSource/{id}")
    @SystemLog(description = "修改数据源",type = LogType.UPDATE)
    public ResponseResult updateDataSource(@RequestBody SourceEntity sourceEntity, @PathVariable("id") Long id) {
        return sourceService.updateDataSource(sourceEntity, id);
    }


    @PostMapping("/testRedis")
    @SystemLog(description = "测试redis连接",type = LogType.OTHER_OPERATIONS)
    public Jedis testRedisDataSource(@RequestBody SourceEntity sourceEntity) {

        Jedis redisConnection = RedisConnention.initPool(sourceEntity);
        System.out.println("redis连接成功-------》》》》》" + redisConnection);

        String s = redisConnection.get("sys_config:sys.index.sideTheme");
        System.out.println(s);
        return redisConnection;
    }


    /**
     * 分页最低10条，最高50条数据
     * 进行DES加密
     */
    @GetMapping("/findPageAll/{pageSize}/{pageNum}/{flag}")
    @SystemLog(description = "分页查询",type = LogType.SELETE)
    public String findPageAll(@PathVariable("pageSize") Integer pageSize,
                              @PathVariable("pageNum") Integer pageNum,
                              @PathVariable("flag") Integer flag) {
        if (flag == 1) {
            //如果传过来是1就加密
            String alldes = sourceService.findPageAll(pageSize, pageNum);
            return alldes.toString();
        }
        if (flag != 1 || flag == null) {
            return sourceService.findPageAll(pageSize, pageNum);
        }
        return null;

    }

    /**
     * 初始化连接池
     */
    @PostConstruct
    @SystemLog(description = "初始化连接池",type = LogType.OTHER_OPERATIONS)
    public void initConnector() {
        List<SourceEntity> list = sourceService.findList();
        JdbcConnector.initJdbcPoolMap(list);
        RedisConnector.initRedisPoolMap(list);
    }

    /**
     * 获取数据源
     * @return 数据源集合
     */
    @GetMapping("/getAllDataSource")
    @SystemLog(description = "获取数据源",type = LogType.SELETE)
    public List<Map<String,Object>> findAllDataCourse() {
        return sourceService.findAllDataSource();
    }

    /**
     * 获取指定数据源所属数据库
     * @return 数据库
     */
    @GetMapping("/getAllDataBase/{dataSourceId}")
    @SystemLog(description = "获取指定数据源所属数据库",type = LogType.SELETE)
    public List<String> findAllDataBase(@PathVariable("dataSourceId") Long dataSourceId) {
        return sourceService.findAllDataBase(dataSourceId);
    }

    /**
     * 获取指定数据源所有表
     * @param dataCourseId 数据源ID
     * @param dataBaseName  数据库名称
     * @return 表集合
     * @throws SQLException
     */
    @GetMapping("/getTablesByDataSource")
    @SystemLog(description = "获取指定数据源所有表",type = LogType.SELETE)
    public List<String> findTablesByDataSource(@RequestParam("dataSourseId")Long dataCourseId,
                                               @RequestParam("dataBaseName")String dataBaseName) throws SQLException {
        return sourceService.findTablesByDataSource(dataCourseId,dataBaseName);
    }

    /**
     * 获取指定数据源
     * @param dataSourceId 数据源ID
     * @return
     */
    @GetMapping("/getDataSourceById/{dataSourceId}")
    @SystemLog(description = "获取指定数据源",type = LogType.SELETE)
    public SourceEntity getDataSourceById(@PathVariable("dataSourceId")Long dataSourceId){
        return sourceService.getDataSourceById(dataSourceId);
    }

    /**
     * 执行sql查询
     * @param dataSourceId 数据库ID
     * @param sql sql语句
     */
    @GetMapping("/executeSqlQuery")
    @SystemLog(description = "执行sql查询",type = LogType.SELETE)
    List<Map<String,Object>> executeSqlQuery(@RequestParam("dataSourceId") Long dataSourceId,
                                             @RequestParam("dataBaseName")String dataBaseName,
                                             @RequestParam("tableName")String tableName,
                                             @RequestParam("sql") String sql){
        return sourceService.executeSqlQuery(dataSourceId,dataBaseName,tableName,sql);
    }

}
